package com.zhuss.opus.invit;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.ListUserPublishBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.opus.invit.DetailInvitBo;
import com.zhuss.bean.bo.opus.invit.SearchInvitBo;
import com.zhuss.bean.bo.opus.invit.wechat.ListInvitBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitVo;
import com.zhuss.bean.vo.opus.invit.DetailNoReadVo;
import com.zhuss.bean.vo.opus.invit.ListInvitVo;
import com.zhuss.common.AbstractBo;
import com.zhuss.common.BasePageParam;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.core.processor.UserAppendRead;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.processor.InvitAppendRead;
import com.zhuss.opus.processor.InvitRead;
import com.zhuss.opus.processor.InvitTopicRead;


/**
 * invit查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("invitReadService")
public class InvitReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("invitRead")
	private InvitRead invitRead;

	@Autowired
	@Qualifier("invitTopicRead")
	private InvitTopicRead invitTopicRead;
	
	@Autowired
	@Qualifier("userAppendRead")
	private UserAppendRead userAppendRead;
	
	@Autowired
	@Qualifier("invitAppendRead")
	private InvitAppendRead invitAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailInvitBo bo = (DetailInvitBo) param;
		Long invitId = bo.getInvitId();
		DetailInvitVo vo = (DetailInvitVo) invitRead.detail(invitId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		transfer(vo, bo.getConcurrentUserId());
		return vo;
	}
	
	private void transfer(DetailInvitVo vo, Long userId) {
		Integer thumbNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), null).size();
		Integer collectNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), null).size();
		Integer forwardNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), null).size();
		Integer recommendNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), null).size();
		Integer topicNum = invitTopicRead.listTopic(vo.getInvitId(), 0l).size();
		
		Integer thumbSelfNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), userId).size();
		Integer collectSelfNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), userId).size();
		Integer forwardSelfNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), userId).size();
		Integer recommendSelfNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), userId).size();
		
		
		vo.setThumbNum(thumbNum);
		vo.setCollectNum(collectNum);
		vo.setForwardNum(forwardNum);
		vo.setRecommendNum(recommendNum);
		vo.setTopicNum(topicNum);
		
		vo.setThumbSelfNum(thumbSelfNum);
		vo.setCollectSelfNum(collectSelfNum);
		vo.setForwardSelfNum(forwardSelfNum);
		vo.setRecommendSelfNum(recommendSelfNum);
		
	}

	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchInvitBo bo = (SearchInvitBo) param;
		Object result = invitRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	
	/**
	 * 获取消息未读数
	 */
	public DetailNoReadVo noReadNum(AbstractBo bo) throws WpsException {
		Integer thumbNum = invitAppendRead.countNoReadNum(bo.getConcurrentUserId(), AppendTypeEnum.点赞.key());
		Integer topicNum = invitTopicRead.countNoReadNum(bo.getConcurrentUserId());
		Integer focusByNum = userAppendRead.countNoReadNum(bo.getConcurrentUserId(), AppendTypeEnum.关注.key());
		DetailNoReadVo result = new DetailNoReadVo();
		result.setThumbNum(thumbNum);
		result.setTopicNum(topicNum);
		result.setFocusByNum(focusByNum);
		return result;
	}
	
	
	
	/**
	 * 点赞
	 * 列表
	 */
	public Object listThumbBy(BasePageParam param) throws WpsException {
		Object result = invitAppendRead.listThumbBy(param);
		return result;
	}
	
	/**
	 * 评论
	 * 列表
	 */
	public Object listTopicBy(BasePageParam param) throws WpsException {
		Object result = invitTopicRead.listTopicBy(param);
		return result;
	}
	
	/**
	 * 我的
	 * 列表
	 */
	public Object listMy(BasePageParam param) throws WpsException {
		ListInvitBo bo = new ListInvitBo();
		BeanUtils.copyProperties(param, bo);
		bo.setCreatorId(param.getConcurrentUserId());
		Object result = invitRead.listInvit(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 别人的
	 * 列表
	 * 
	 */
	public Object listUserPublish(ListUserPublishBo param) throws WpsException {
		ListInvitBo bo = new ListInvitBo();
		BeanUtils.copyProperties(param, bo);
		bo.setCreatorId(param.getUserId());
		Object result = invitRead.listInvit(bo);
		return result;
	}
	
	/**
	 * 我的
	 * 收藏
	 */
	public Object listMyCollect(BasePageParam param) throws WpsException {
		ListInvitBo bo = new ListInvitBo();
		BeanUtils.copyProperties(param, bo);
		bo.setCreatorId(param.getConcurrentUserId());
		Object result = invitAppendRead.listCollect2(param);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailInvitBo param) throws WpsException {
		Long invitId = param.getInvitId();
		Object result = invitTopicRead.listTopicTree(invitId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listInvit(Object param) throws WpsException {
		ListInvitBo bo = (ListInvitBo) param;
		Object result = invitRead.listInvit(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailInvitVo> pageVo = (PageVo<DetailInvitVo>) result;
		for (DetailInvitVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), null).size();
			Integer collectNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), null).size();
			Integer forwardNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), null).size();
			Integer recommendNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), null).size();
			Integer topicNum = invitTopicRead.listTopic(vo.getInvitId(), 0l).size();
			
			Integer thumbSelfNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), userId).size();
			Integer collectSelfNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), userId).size();
			Integer forwardSelfNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), userId).size();
			Integer recommendSelfNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListInvitVo> pageVo = (PageVo<ListInvitVo>) result;
		for (ListInvitVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), null).size();
			Integer collectNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), null).size();
			Integer forwardNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), null).size();
			Integer recommendNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), null).size();
			Integer topicNum = invitTopicRead.listTopic(vo.getInvitId(), 0l).size();
			
			Integer thumbSelfNum = invitAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getInvitId(), userId).size();
			Integer collectSelfNum = invitAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getInvitId(), userId).size();
			Integer forwardSelfNum = invitAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getInvitId(), userId).size();
			Integer recommendSelfNum = invitAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getInvitId(), userId).size();
			
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
			
		}
	}
	
	
}

